
GBody2d sun, earth, satelite, moon;
float timestep = 1.0/10.0;
float time;

void setup()
{
  size(800,800);
  
  earth = new GBody2d();
  satelite = new GBody2d();
  sun = new GBody2d();
  moon = new GBody2d();

  sun.xpos = 500;
  sun.ypos = 500;
  sun.xvel = 0;
  sun.yvel = 0;
  sun.mass = 6000000;
  sun.radius = 10;

  earth.xpos = 300;
  earth.ypos = 300;
  earth.xvel = 100;
  earth.yvel = 0;
  earth.mass = 100000;
  earth.radius = 10;

  frameRate(30);  

}

void draw() 
{
  background(0);
  sun.display();
  earth.display();
  sun.addForce(earth);
  earth.addForce(sun);
  earth.calcNewState();
  sun.calcNewState();
  sun.printVelVector();
  sun.printAccVector();
  earth.printVelVector();
  earth.printAccVector();
  sun.resetForce();
  earth.resetForce();
}

void mouseMove(){
  

class GBody2d
{
  public float xpos;
  public float ypos;
  public float xvel;
  public float yvel;
  public float xacc = 0;
  public float yacc = 0;  
  public float mass;
  public float radius;

  void display(){
    ellipse(xpos,ypos,radius,radius);
  }  

  void addForce(GBody2d otherbody){

    float radius = pow(pow(otherbody.ypos - ypos,2)+pow(otherbody.xpos - xpos,2),0.5);
    float Gacc = otherbody.mass/(pow(radius,2));
    float angle = atan2((otherbody.ypos - ypos),(otherbody.xpos - xpos));
    xacc = xacc + Gacc*cos(angle);
    yacc = yacc + Gacc*sin(angle);
  }

  void resetForce(){
    xacc = 0;
    yacc = 0;
  }

  void calcNewState(){
    xvel = xvel + xacc*timestep;
    yvel = yvel + yacc*timestep;
    xpos = xpos + timestep * xvel;
    ypos = ypos + timestep * yvel;  
  }

  void printVelVector(){
    float k = 2;
    float xpnt = xvel*k + xpos;
    float ypnt = yvel*k + ypos;
    float velMag = pow(pow(xvel,2)+pow(yvel,2),0.5);
    stroke(0,255,255);
    arrow(xpos,ypos,xpnt,ypnt,velMag/5);
    noStroke();

  }

  void printAccVector(){
    float k = 2;
    float accMag = pow(pow(xacc,2)+pow(yacc,2),0.5);
    float xpnt = xacc*k + xpos;
    float ypnt = yacc*k + ypos;
    stroke(255,0,255);
    arrow(xpos,ypos,xpnt,ypnt,accMag/5);
    noStroke();

  }

  void arrow(float x1, float y1, float x2, float y2,float sizeAr) {
    line(x1, y1, x2, y2);
    pushMatrix();
    translate(x2, y2);
    float a = atan2(x1-x2, y2-y1);
    rotate(a);
    line(0, 0, -sizeAr, -sizeAr);
    line(0, 0, sizeAr, -sizeAr);
    popMatrix();
  }

  void showVector(){

  }

}


